package com.ax.base.system.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.base.common.dao.BaseAreaMapper;
import com.ax.base.common.domain.BaseArea;
import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.BuildTree;
import com.ax.base.common.utils.PageUtils;
import com.ax.base.common.utils.PinYinUtil;
import com.ax.base.common.utils.Query;
import com.ax.base.system.dao.MenuMapper;
import com.ax.base.system.domain.MenuDO;
import com.ax.base.system.service.AreaService;

@Service
public class AreaServiceImpl implements AreaService {

	@Autowired
	BaseAreaMapper baseAreaMapper;
	
	@Autowired
	MenuMapper menuMapper;
	
	@Override
	public Tree<MenuDO> list() {
		List<Tree<MenuDO>> areas = baseAreaMapper.listArea();
		Tree<MenuDO> t = BuildTree.build(areas);
		return t;
	}
    
	/**
	 * 行政规划查询(模块:行政规划)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseArea>
	 */
	@Override
	public PageUtils queryArealistSer(Map<String, Object> params) {
		Query query = new Query(params);
		List<BaseArea> BaseAreas = menuMapper.queryArealistMapper(query);
		int total = menuMapper.count(query);
		PageUtils pageUtil = new PageUtils(BaseAreas, total);
		return pageUtil;
	}

	/**
	 * 行政规划新增(模块:行政规划)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return BaseArea
	 */
	@Override
	public BaseArea getArea(Long areaId) {
		return menuMapper.getAreaMapper(areaId);
	}

	/**
	 * 行政规划新增(模块:行政规划)
	 * 
	 * @param BaseArea baseArea
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertAreaSer(BaseArea baseArea) {
		baseArea.setFirstLetter(PinYinUtil.getFirstSpell(baseArea.getAreaName()));
		menuMapper.insertAreaMapper(baseArea);
	}

	/**
	 * 行政规划修改(模块:行政规划)
	 * 
	 * @param BaseArea baseArea
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateAreaSer(BaseArea baseArea) {
		baseArea.setFirstLetter(PinYinUtil.getFirstSpell(baseArea.getAreaName()));
		menuMapper.updateAreaMapper(baseArea);
	}

	/**
	 * Tree
	 * 
	 * @param Long parentAreaId
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getAreaTree(Long parentAreaId) {
		String pareaId = parentAreaId.toString();
		// 根据roleId查询权限
		List<Tree<MenuDO>> deps = menuMapper.listPareaId();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (id.equals(pareaId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}
	
	
}
